import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


//VS4E -- DO NOT REMOVE THIS LINE!
public class CreatePlayerFrame extends JFrame implements Runnable {

	private static final long serialVersionUID = 1L;	
	static Player player = new Player();
	static Player arrayedPlayer[] = new Player[8];
	static Player orderedPlayer[] = new Player[8];
	static Random random = new Random();

	static int storage;
	static boolean willRepeat=false;
	static int x=0;
	private static int[] temporaryOrder = new int[8];
	static int b = 0;
	static int numberOfPlayers = 0;
	
	static int shufflesLeft = 0;
	Thread t;
	public boolean isShuffling = false;
	
	private JTextField player1NameTextField;
	private JButton addPlayer1Button;
	private JTextField player2NameTextField;
	private JButton addPlayer2Button;
	private JTextField player3NameTextField;
	private JButton addPlayer3Button;
	private JTextField player4NameTextField;
	private JButton addPlayer4Button;
	private JTextField player5NameTextField;
	private JButton addPlayer5Button;
	private JTextField player6NameTextField;
	private JButton addPlayer6Button;
	private JTextField player7NameTextField;
	private JButton addPlayer7Button;
	private JTextField player8NameTextField;
	private JButton addPlayer8Button;
	private JButton startGameButton;
	private JLabel background;
	private JLabel pNumber1Label;
	private JLabel pNumber2Label;
	private JLabel pNumber3Label;
	private JLabel pNumber4Label;
	private JLabel pNumber5Label;
	private JLabel pNumber6Label;
	private JLabel pNumber7Label;
	private JLabel pNumber8Label;
	private JButton shufflePlayersButton;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public CreatePlayerFrame() {
		initComponents();
	}

	private void initComponents() {
		setTitle("Monopoly");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon.png")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(shufflePlayersButton(), new Constraints(new Leading(540, 175, 10, 10), new Leading(330, 80, 10, 10)));
		add(addPlayer1Button(), new Constraints(new Leading(100, 12, 12), new Leading(60, 12, 12)));
		add(addPlayer2Button(), new Constraints(new Leading(350, 12, 12), new Leading(60, 12, 12)));
		add(addPlayer3Button(), new Constraints(new Leading(600, 12, 12), new Leading(60, 12, 12)));
		add(addPlayer4Button(), new Constraints(new Leading(100, 12, 12), new Leading(210, 12, 12)));
		add(addPlayer5Button(), new Constraints(new Leading(350, 12, 12), new Leading(210, 12, 12)));
		add(addPlayer6Button(), new Constraints(new Leading(600, 12, 12), new Leading(210, 12, 12)));
		add(addPlayer7Button(), new Constraints(new Leading(100, 12, 12), new Leading(360, 12, 12)));
		add(addPlayer8Button(), new Constraints(new Leading(350, 12, 12), new Leading(360, 12, 12)));
		add(player1NameTextField(), new Constraints(new Leading(100, 96, 12, 12), new Leading(35, 12, 12)));
		add(player2NameTextField(), new Constraints(new Leading(350, 96, 12, 12), new Leading(35, 12, 12)));
		add(player3NameTextField(), new Constraints(new Leading(600, 94, 12, 12), new Leading(35, 12, 12)));
		add(player4NameTextField(), new Constraints(new Leading(100, 94, 12, 12), new Leading(185, 12, 12)));
		add(player5NameTextField(), new Constraints(new Leading(350, 94, 12, 12), new Leading(185, 12, 12)));
		add(player6NameTextField(), new Constraints(new Leading(600, 94, 12, 12), new Leading(185, 12, 12)));
		add(player7NameTextField(), new Constraints(new Leading(100, 94, 12, 12), new Leading(335, 12, 12)));
		add(player8NameTextField(), new Constraints(new Leading(350, 94, 12, 12), new Leading(335, 12, 12)));
		add(startGameButton(), new Constraints(new Leading(540, 175, 10, 10), new Leading(330, 80, 10, 10)));
		add(pNumber1Label(), new Constraints(new Leading(200, 10, 10), new Leading(60, 10, 10)));
		add(pNumber2Label(), new Constraints(new Leading(450, 10, 10), new Leading(60, 10, 10)));
		add(pNumber3Label(), new Constraints(new Leading(700, 10, 10), new Leading(60, 10, 10)));
		add(pNumber4Label(), new Constraints(new Leading(200, 10, 10), new Leading(210, 10, 10)));
		add(pNumber5Label(), new Constraints(new Leading(450, 10, 10), new Leading(210, 10, 10)));
		add(pNumber6Label(), new Constraints(new Leading(700, 10, 10), new Leading(210, 10, 10)));
		add(pNumber7Label(), new Constraints(new Leading(200, 10, 10), new Leading(360, 10, 10)));
		add(pNumber8Label(), new Constraints(new Leading(450, 10, 10), new Leading(360, 10, 10)));
		add(background(), new Constraints(new Leading(0, 12, 12), new Leading(0, 12, 12)));
		setSize(750, 450);
		setDefaultCloseOperation(CreatePlayerFrame.EXIT_ON_CLOSE);
		getContentPane().setPreferredSize(getSize());
		pack();
		setLocationRelativeTo(null);
		setVisible(false);
	}
	
	private JLabel background() {
		if (background == null) {
			background = new JLabel();
			background.setIcon(new ImageIcon(getClass().getResource("/images/createPlayer.jpg")));
		}
		return background;
	}
	
	private JButton shufflePlayersButton() {
		if (shufflePlayersButton == null) {
			shufflePlayersButton = new JButton();
			shufflePlayersButton.setText("Shuffle Players");
			shufflePlayersButton.setEnabled(false);
			shufflePlayersButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					shufflePlayersButtonActionActionPerformed(event);
				}
			});
		}
		return shufflePlayersButton;
	}

	private JLabel pNumber1Label() {
		if (pNumber1Label == null) {
			pNumber1Label = new JLabel();
			pNumber1Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber1Label.setForeground(Color.white);
		}
		return pNumber1Label;
	}
	
	private JLabel pNumber2Label() {
		if (pNumber2Label == null) {
			pNumber2Label = new JLabel();
			pNumber2Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber2Label.setForeground(Color.white);
		}
		return pNumber2Label;
	}
	private JLabel pNumber3Label() {
		if (pNumber3Label == null) {
			pNumber3Label = new JLabel();
			pNumber3Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber3Label.setForeground(Color.white);
		}
		return pNumber3Label;
	}
	private JLabel pNumber4Label() {
		if (pNumber4Label == null) {
			pNumber4Label = new JLabel();
			pNumber4Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber4Label.setForeground(Color.white);
		}
		return pNumber4Label;
	}
	private JLabel pNumber5Label() {
		if (pNumber5Label == null) {
			pNumber5Label = new JLabel();
			pNumber5Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber5Label.setForeground(Color.white);
		}
		return pNumber5Label;
	}
	private JLabel pNumber6Label() {
		if (pNumber6Label == null) {
			pNumber6Label = new JLabel();
			pNumber6Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber6Label.setForeground(Color.white);
		}
		return pNumber6Label;
	}
	private JLabel pNumber7Label() {
		if (pNumber7Label == null) {
			pNumber7Label = new JLabel();
			pNumber7Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber7Label.setForeground(Color.white);
		}
		return pNumber7Label;
	}
	private JLabel pNumber8Label() {
		if (pNumber8Label == null) {
			pNumber8Label = new JLabel();
			pNumber8Label.setFont(new Font("Agency FB", Font.BOLD, 16));
			pNumber8Label.setForeground(Color.white);
		}
		return pNumber8Label;
	}

	private JButton startGameButton() {
		if (startGameButton == null) {
			startGameButton = new JButton();
			startGameButton.setText("Proceed to Game");
			startGameButton.setEnabled(false);
			startGameButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					startGameButtonActionActionPerformed(event);
				}
			});
		}
		return startGameButton;
	}

	private JButton addPlayer8Button() {
		if (addPlayer8Button == null) {
			addPlayer8Button = new JButton();
			addPlayer8Button.setText("Add player");
			addPlayer8Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer8ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer8Button;
	}

	private JTextField player8NameTextField() {
		if (player8NameTextField == null) {
			player8NameTextField = new JTextField();
			player8NameTextField.setText("Watari");
		}
		return player8NameTextField;
	}

	private JButton addPlayer7Button() {
		if (addPlayer7Button == null) {
			addPlayer7Button = new JButton();
			addPlayer7Button.setText("Add player");
			addPlayer7Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer7ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer7Button;
	}

	private JTextField player7NameTextField() {
		if (player7NameTextField == null) {
			player7NameTextField = new JTextField();
			player7NameTextField.setText("Teru Mikami");
		}
		return player7NameTextField;
	}

	private JButton addPlayer6Button() {
		if (addPlayer6Button == null) {
			addPlayer6Button = new JButton();
			addPlayer6Button.setText("Add player");
			addPlayer6Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer6ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer6Button;
	}

	private JTextField player6NameTextField() {
		if (player6NameTextField == null) {
			player6NameTextField = new JTextField();
			player6NameTextField.setText("Mihael Keehl");
		}
		return player6NameTextField;
	}

	private JButton addPlayer5Button() {
		if (addPlayer5Button == null) {
			addPlayer5Button = new JButton();
			addPlayer5Button.setText("Add player");
			addPlayer5Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer5ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer5Button;
	}

	private JTextField player5NameTextField() {
		if (player5NameTextField == null) {
			player5NameTextField = new JTextField();
			player5NameTextField.setText("Nate River");
		}
		return player5NameTextField;
	}

	private JButton addPlayer4Button() {
		if (addPlayer4Button == null) {
			addPlayer4Button = new JButton();
			addPlayer4Button.setText("Add player");
			addPlayer4Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer4ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer4Button;
	}

	private JTextField player4NameTextField() {
		if (player4NameTextField == null) {
			player4NameTextField = new JTextField();
			player4NameTextField.setText("Ryuk");
		}
		return player4NameTextField;
	}

	private JButton addPlayer3Button() {
		if (addPlayer3Button == null) {
			addPlayer3Button = new JButton();
			addPlayer3Button.setText("Add player");
			addPlayer3Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer3ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer3Button;
	}

	private JTextField player3NameTextField() {
		if (player3NameTextField == null) {
			player3NameTextField = new JTextField();
			player3NameTextField.setText("Misa Amane");
		}
		return player3NameTextField;
	}

	private JButton addPlayer2Button() {
		if (addPlayer2Button == null) {
			addPlayer2Button = new JButton();
			addPlayer2Button.setText("Add player");
			addPlayer2Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer2ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer2Button;
	}

	private JTextField player2NameTextField() {
		if (player2NameTextField == null) {
			player2NameTextField = new JTextField();
			player2NameTextField.setText("L Lawliet");
		}
		return player2NameTextField;
	}

	private JButton addPlayer1Button() {
		if (addPlayer1Button == null) {
			addPlayer1Button = new JButton();
			addPlayer1Button.setText("Add player");
			addPlayer1Button.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					addPlayer1ButtonActionActionPerformed(event);
				}
			});
		}
		return addPlayer1Button;
	}

	private JTextField player1NameTextField() {
		if (player1NameTextField == null) {
			player1NameTextField = new JTextField();
			player1NameTextField.setText("Yagami Light");
		}
		return player1NameTextField;
	}

	
//THREAD

	@SuppressWarnings("static-access")
	
	public void run() {
		shufflesLeft = 20;
		while(isShuffling){
			if(shufflesLeft!=0){
				for(int i=0;i<numberOfPlayers;i++){
					if(orderedPlayer[i].getAssignedLabel()==1)
						pNumber1Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==2)
						pNumber2Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==3)
						pNumber3Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==4)
						pNumber4Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==5)
						pNumber5Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==6)
						pNumber6Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==7)
						pNumber7Label.setText("P"+(random.nextInt(7)+1));
					if(orderedPlayer[i].getAssignedLabel()==8)
						pNumber8Label.setText("P"+(random.nextInt(7)+1));
				}
				try
				{
					t.sleep(50);
				}catch(InterruptedException e){}
				shufflesLeft--;
				
				if(shufflesLeft==0){
					startGameButton.setEnabled(true);
					isShuffling = false;
					updateAll();
				}
			}
		}
		
	}

//FUNCTIONS

	public void updateAll() {
		for(int i=0;i<numberOfPlayers;i++){
			if(orderedPlayer[i].getAssignedLabel()==1)
			pNumber1Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==2)
			pNumber2Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==3)
			pNumber3Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==4)
			pNumber4Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==5)
			pNumber5Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==6)
			pNumber6Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==7)
			pNumber7Label.setText("P"+(i+1));
			if(orderedPlayer[i].getAssignedLabel()==8)
			pNumber8Label.setText("P"+(i+1));
		}
	}
	
	public static void insertOrder(int ex){					//function that will insert a number into the attribute order in a player class 
		int y=1;											//depending on the assigned temporary number to them
		for(int z=1;z<=8;z++){
			for(int x = 0; x<=ex; x++){
				if(arrayedPlayer[x].getTemporaryOrder() == z){
					arrayedPlayer[x].setOrder(y);
					y++;
				}
			}
		}
	}

	public static void rearrangeArray(int dx){				//function that will rearrange arrays according to their temporaryOrder
		int y=0;
		for(int z=1;z<=8;z++){
			for(int x=0;x<=dx;x++){
				if(arrayedPlayer[x].getOrder()==z){
					orderedPlayer[y] = arrayedPlayer[x];
					y++;
				}
			}
		}
	}

	public static void assignRandomNumber(){
		do{
			storage=random.nextInt(8)+1; //generate random number
			for(int y=0;y<=7;y++){
				if(temporaryOrder[y] == storage){
					y=8;
					willRepeat=true; //adding temporary order to the created player
					}
				else{
					willRepeat = false;
				}
			}
			temporaryOrder[x] = storage; //when the temporary order doesn't have a pair, it is saved       
		}
		while(willRepeat==true);
		arrayedPlayer[x].setTemporaryOrder(temporaryOrder[x]);
		b = x;
		x++;
		numberOfPlayers = x;
	}
	
	public boolean verifyInput(String input){
		boolean isStringValid = true;
		if(input.length()>=18 || input.length()==0){
			isStringValid = false;
		}
		return isStringValid;
	}
	
//BUTTON ACTIONS
	
	@SuppressWarnings("unused")
	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	private void addPlayer1ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player1NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player1NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(1);
			assignRandomNumber();
			addPlayer1Button.setEnabled(false);
			player1NameTextField.setEnabled(false);
			checkMinimumPlayers();
		}
	}

	private void addPlayer2ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player2NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player2NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(2);
			assignRandomNumber();
			addPlayer2Button.setEnabled(false);
			checkMinimumPlayers();
			player2NameTextField.setEnabled(false);
		}
	}

	private void addPlayer3ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player3NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player3NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(3);
			assignRandomNumber();
			addPlayer3Button.setEnabled(false);
			checkMinimumPlayers();
			player3NameTextField.setEnabled(false);
		}
	}

	private void addPlayer4ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player4NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player4NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(4);
			assignRandomNumber();
			addPlayer4Button.setEnabled(false);
			checkMinimumPlayers();
			player4NameTextField.setEnabled(false);
		}
	}

	private void addPlayer5ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player5NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player5NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(5);
			assignRandomNumber();
			addPlayer5Button.setEnabled(false);
			checkMinimumPlayers();
			player5NameTextField.setEnabled(false);
		}
	}

	private void addPlayer6ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player6NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player6NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(6);
			assignRandomNumber();
			addPlayer6Button.setEnabled(false);
			checkMinimumPlayers();
			player6NameTextField.setEnabled(false);
		}
	}

	private void addPlayer7ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player7NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player7NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(7);
			assignRandomNumber();
			addPlayer7Button.setEnabled(false);
			checkMinimumPlayers();
			player7NameTextField.setEnabled(false);
		}
	}

	private void addPlayer8ButtonActionActionPerformed(ActionEvent event) {
		if(verifyInput(player8NameTextField.getText())){
			arrayedPlayer[x] = player.createPlayer(player8NameTextField.getText());
			arrayedPlayer[x].setAssignedLabel(8);
			assignRandomNumber();
			addPlayer8Button.setEnabled(false);
			checkMinimumPlayers();
			player8NameTextField.setEnabled(false);
		}
	}

	public void checkMinimumPlayers(){
		if(x>=2)
		shufflePlayersButton.setEnabled(true);
	}
	
	private void startGameButtonActionActionPerformed(ActionEvent event) {
		MainFrame.startGame();
		this.dispose();
	}

	private void shufflePlayersButtonActionActionPerformed(ActionEvent event) {
		shufflePlayersButton.setVisible(false);
		run();
		t = new Thread(this);
		t.start();
		isShuffling = true;
		
		insertOrder(b);
		rearrangeArray(b);
		addPlayer1Button.setEnabled(false);
		addPlayer2Button.setEnabled(false);
		addPlayer3Button.setEnabled(false);
		addPlayer4Button.setEnabled(false);
		addPlayer5Button.setEnabled(false);
		addPlayer6Button.setEnabled(false);
		addPlayer7Button.setEnabled(false);
		addPlayer8Button.setEnabled(false);
		player1NameTextField.setEnabled(false);
		player2NameTextField.setEnabled(false);
		player3NameTextField.setEnabled(false);
		player4NameTextField.setEnabled(false);
		player5NameTextField.setEnabled(false);
		player6NameTextField.setEnabled(false);
		player7NameTextField.setEnabled(false);
		player8NameTextField.setEnabled(false);
	}

}
